package insight.google.game.era.bo.policy.gamecard;

import insight.google.game.era.bo.GameActionLog;
import insight.google.game.era.bo.Task;

import insight.google.game.era.bo.GameRoom;

import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.policy.AbstractPolicy;
import insight.google.game.era.bo.policy.PolicyCenter;
import insight.google.game.era.client.proxy.PlayerClientProxy;
import insight.google.game.era.client.proxy.PlayerClientProxyImpl;
import insight.google.game.era.core.hub.HubServiceManager;
import insight.google.game.era.core.hub.PendingEvents;
import insight.google.game.era.core.hub.UserEvents;
import insight.google.game.era.data.Player;
import insight.google.game.era.mgr.GameLogManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AttackPolicy extends AbstractPolicy {

	/**
	 * 
	 */
	private PlayerClientProxy playerClientProxy;
	private static final Log log = LogFactory.getLog(AttackPolicy.class);

	@Override
	public boolean execute(GameUser original, GameUser[] targets, Map params) {
		// TODO Auto-generated method stub
		// started monirul for game log
		GameRoom currentRoom = (GameRoom) original.getCurrentRoom();
		GameLogManager gameLogManager = currentRoom.getGameLogManager();
		// ended monirul for game log

		playerClientProxy = PlayerClientProxyImpl.getInstance();

		PolicyCenter pCenter = PolicyCenter.getInstance();
		System.out.println("PolicyCenter " + pCenter.toString());

		boolean rtnValue = false;
		// check its match card or not.
		List<Task> taskList = currentRoom.getTaskList();
		log.debug("PolicyCenter rask list " + taskList);
		int index = 0;
//		if (taskList != null && !taskList.isEmpty()) {
//			for (Task task : taskList) {
//				String matchValue = task.getMatchCardValue();
//						
//				String trgUser = task.getMatchCardValue();
//				if (matchValue.equals(PolicyCenter.CARD_TYPE_ATTACK)
//						&& trgUser.equals(original.getName())) {
//					rtnValue = true;
//					//gameLogManager.addGameLog(new GameActionLog(original.getName()+ " has played attack card "));
//					break;
//				}
//				index++;
//			}
//			log.debug("return value ### " + rtnValue);
//			if (rtnValue) {
//				if (index < taskList.size() - 1) {
//					// get match data
//					Task nexTask = taskList.get(index + 1);
//					String orgUser = nexTask.getOrgUserName();
//					String trgUserName = nexTask.getTargetUserName();
//					String orgCard = (String) nexTask.getOrgCardId();
//
//					// get match data
//					Map matchData = (Map) pCenter.getPolicyDataMap(orgCard);
//					String matchMethod = (String) matchData
//							.get(PolicyCenter.KEY_METHOD);
//					// get js component id
//					String jsComponentId = (String) params
//							.get("jsComponenetId");
//
//					// set target user active card
//					List<String> activeCards = new ArrayList<String>();
//					activeCards.add("attack");
//					// trgUser.setActiveCards(activeCards);
//					// playerClientProxy.setActiveCard(trgUser.getScriptProxy(),
//					// jsComponenetId, trgUser.getActiveCards());
//					//                    
//					// //set target user active
//					// playerClientProxy.setActive(trgUser.getScriptProxy(),
//					// jsComponenetId, true);
//					
//					GameUser trgUser = currentRoom.getUserByName(trgUserName);
//					trgUser.setActiveCards(activeCards, jsComponentId);
//					trgUser.setClientActive(true, jsComponentId);
//
//					// let target user do match card
//					// playerClientProxy.attackCardPolicyExe(trgUser.getScriptProxy(),
//					// jsComponenetId, matchMethod, original.getName(),
//					// cardName);
//					// playerClientProxy.duelCardPolicyExe(trgUser.getScriptProxy(),
//					// jsComponentId, matchMethod, original.getName(), orgCard);
//					trgUser.dualCardPolicyExecute(orgCard, jsComponentId,
//							matchMethod, original.getName());
//
//					//gameLogManager.addGameLog(new GameActionLog(original.getName()+ " attacked user " + trgUser.getName()));
//
//				} else {
//
//				}
//				currentRoom.removeTask(index);
//			}
//		} else {
			for (int i = 0; i < targets.length; i++) {
				GameUser trgUser = targets[i];

				System.out.println("original.getName() " + original.getName());
				System.out.println("trgUser.getName() " + trgUser.getName());
				
				currentRoom.putTask(original, trgUser,
						PolicyCenter.CARD_TYPE_ATTACK,
						PolicyCenter.CARD_TYPE_DEFEND);

				// get match data
				String cardName = (String) params.get("cardName");
				String cardId = (String) params.get("cardId");
				Map matchData = (Map) pCenter.getPolicyDataMap(cardName);
				String matchMethod = (String) matchData
						.get(PolicyCenter.KEY_METHOD);
				// get js component id
				String jsComponentId = (String) params.get("jsComponenetId");

				// check target user's YinYangShield
				// trgUser.getEquipmentCards();

				// set target user active card
				List<String> activeCards = new ArrayList<String>();
				activeCards.add("defend");

				// implement for new design start
				HashMap<String, Object> data = new HashMap<String, Object>();
				data.put("cardName", cardName);
				data.put("matchMethod", matchMethod);
				PendingEvents pendingEvent = new PendingEvents();
				pendingEvent.setPendingUser(trgUser);
				pendingEvent.setSendUserId(original.getName());
				pendingEvent.setTargetUserId(original.getName());
				pendingEvent.setCardId(cardId);
				pendingEvent.setCardName(cardName);
				pendingEvent.setParamList1(activeCards);
				pendingEvent.setFunctionName(UserEvents.PLAYERPROXY_SETACTIVECARD.toString());
				pendingEvent.setJsComponentId(jsComponentId);
				HubServiceManager.getInstance().addPendingEvent(trgUser, pendingEvent);

				PendingEvents pendingEvent2 = new PendingEvents();
				pendingEvent2.setPendingUser(trgUser);
				pendingEvent2.setSendUserId(original.getName());
				pendingEvent2.setTargetUserId(original.getName());
				pendingEvent2.setCardId(cardId);
				pendingEvent2.setCardName(cardName);
				pendingEvent2.setParamStr1("true");
				pendingEvent2.setFunctionName(UserEvents.PLAYERPROXY_SETACTIVE.toString());
				pendingEvent2.setJsComponentId(jsComponentId);
				HubServiceManager.getInstance().addPendingEvent(trgUser, pendingEvent2);

				PendingEvents pendingEvent1 = new PendingEvents();
				pendingEvent1.setPendingUser(trgUser);
				pendingEvent1.setCardId(cardId);
				pendingEvent1.setCardName(cardName);
				pendingEvent1.setSendUserId(original.getName());
				pendingEvent1.setTargetUserId(trgUser.getName());
				pendingEvent1.setCallbackFunctionName(matchMethod);
				pendingEvent1.setFunctionName(UserEvents.PLAYERPROXY_ATACKCARD_POLICYEXECUTE.toString());
				pendingEvent1.setJsComponentId(jsComponentId);
				HubServiceManager.getInstance().addPendingEvent(
						trgUser, pendingEvent1);
				
				trgUser.setHavePendingEvent(true);
				trgUser.setActive(true);

				// trgUser.setActiveCards(activeCards,jsComponentId);
				// end
				// check and set current user to hangUser,
				original.setHanged(true);
				if (currentRoom.getHangUser() == null) {
					currentRoom.setHangUser(original);
				}
				activeCards = original.getActiveCards();
				activeCards.remove("attack");

				// trgUser.setActiveCards(activeCards);
				// playerClientProxy.setActiveCard(trgUser.getScriptProxy(),
				// jsComponenetId, trgUser.getActiveCards());
				//                
				// //set target user active
				// playerClientProxy.setActive(trgUser.getScriptProxy(),
				// jsComponenetId, true);

				// trgUser.setActive(true, jsComponentId);

				// let target user do match card
				// playerClientProxy.attackCardPolicyExe(trgUser.getScriptProxy(),
				// jsComponenetId, matchMethod, original.getName(), cardName);
				// trgUser.attackCardPolicyExe(cardName, jsComponentId,
				// matchMethod, original.getName());
				// remove original user attack premission
				// set target user active card
				// activeCards = new ArrayList<String>();
				// for(int j=0; j<GameRoom.initActiveCards.length; j++){
				// activeCards.add(GameRoom.initActiveCards[j]);
				// }
				// activeCards = original.getActiveCards();
				// activeCards.remove("attack");
				original.setActiveCards(activeCards);
				playerClientProxy.setActiveCard(original.getScriptProxy(),
						jsComponentId, original.getActiveCards());
				// trgUser.setActiveCards(activeCards,jsComponentId);
				// trgUser.setActive(true, jsComponentId);
				gameLogManager.addGameLog(new GameActionLog(original.getName()+ " attacked user " + targets[i].getName()));

//			}
		}
		return rtnValue;
	}

	public void doMatchAttackCard() {

	}

}
